#!/usr/bin/awk -f
BEGIN {FS =","
	OFS=","
hdop=-9
utc=-9
rms=-9
gsat=""
gsview="{"
valid="V"
pdop=-9}
/RMC/ {
	if (valid != "V" ) {
		if (length(gsview)>1){
			gsview=gsview",\"SAT\":{"gsat"}}"
		}
		print gsview
	}
	hdop=-9
	height=-9
	geoid=-9
	gsview="{"
	gsat=""
	valid=$3
	if(valid != "V"){
		split($1,tel,"$")
		split($2,zeit,".")
		split($4,w,".")
		for(i=length(w[2]);i<6;i++)w[2]=w[2] "0";
		deg=substr(w[1],1,length(w[1])-2)*60+substr(w[1],length(w[1])-1,2)
		ndeg=""
		if($5=="S")ndeg="-"
		split($6,l,".")
		for(i=length(l[2]);i<6;i++)l[2]=l[2] "0";
		ldeg=substr(l[1],1,length(l[1])-2)*60+substr(l[1],length(l[1])-1,2)
		nldeg=""
		if($7=="W")nldeg="-"
		gsview=gsview"\"UTC\":"mktime("20"substr($10,5,2)" "substr($10,3,2)" "substr($10,1,2)" "substr($2,1,2)" "substr($2,3,2)" "substr($2,5,2))
		gsview=gsview",\"TYPE\":\""substr(tel[2],2,1)"\""
		gsview=gsview",\"LAT\":"ndeg deg w[2]
		gsview=gsview",\"LON\":"nldeg ldeg l[2]
		gsview=gsview",\"SPEED\":"$8*514444
		if($9="")$9=0
		gsview=gsview",\"COURSE\":"$9+0
	}
	}
/GGA/ {
	gsview=gsview",\"HDOP\":"$9*1000
	gsview=gsview",\"ALT\":"$10*1000
	gsview=gsview",\"GEOID\":"$12*1000
	}
/GSA/ {
	gsview=gsview",\"PDOP\":"$16*1000
	gsview=gsview",\"VDOP\":"$18*1000
}
/GRS/ {
	tgrs=""
	for(i=4;i<=15;i++){
		if($i!="")tgrs=tgrs","
	}
	if(tgrs!=""){
	gsview=gsview",\"GRS\":{"tgrs"}"
}
}
/GSV/ { satfield= NF - 4
        satcount= int(satfield / 4)
	sati=(satcount<(satfield/4)?satcount+1:satcount)
	for(i=1;i<=sati;i++){
		if(i*4+4<=NF){
			if(length($(i*4+4))==0){$(i*4+4)=0}
			if($(i*4+4)=="\r"){$(i*4+4)=0}
			gsat=gsat "\""substr($1,3,1)$(4*i+1)"\":{\"EL\":"$(4*i+2)",\"AZ\":"$(4*i+3)",\"SN\":"$(4*i+4)"},"
			}
	}
	}
/GST/ {
	gsview=gsview",\"RMS\":"$4
	gsview=gsview",\"LATSTD\":"$8*1000
	gsview=gsview",\"LONSTD\":"$9*1000
	gsview=gsview",\"ALTSTD\":"$10*1000
	}
/GBS/ {
	gsview=gsview",\"LATERR\":"$3*1000
	gsview=gsview",\"LONERR\":"$4*1000
	gsview=gsview",\"ALTERR\":"$5*1000
	}
